<?php
namespace api\xcx\controller;
use think\Cache;
use think\Loader;
use think\Db;
use think\Request;
use think\Controller;
use think\config;
class User extends Base
/**
 *  User.php 用户控制器
 *
 * @copyright			(C) 2014-2018 ShengTeng
 * @lastmodify			2018-05-21
 */

{
   /**
 * @api {POST} User/updateInfo  完善资料
 * @apiVersion 1.0.0
 * @apiGroup 用户
 *
 * @apiParam {String} token 用户标识
 * @apiParam {String} name 姓名
 * @apiParam {String} phone 手机号
 * @apiParam {String} address 地址 
 *
 * @apiSuccess {Int} code 返回码
 * @apiSuccess {String} message  提示信息
 * @apiSuccess {Object} data  返回数据
 *
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *          "code":0,
 *          "message":"",
 *          "data":''
 *      }
 */
   public function updateInfo(Request $request){
        $params=$request->param();
        
        //token
        $token = $this->checkParam($params['token'],'token',true);
        //address
        $address = $this->checkParam($params['address'],'address',true);
        //name
        $name = $this->checkParam($params['name'],'name',true);
        // phone
        $phone = $this->checkParam($params['phone'],'phone',true);

        $user = Model('User');
        $res = $user->updateInfo($token,$name,$phone,$address);
        if($res){
            return json(returnAjax());
        }else{
            return json(returnAjax('','系统异常','-1'));
        }
   }

    /**
     * @api {POST} User/getUserInfo  获取用户信息
     * @apiVersion 1.0.0
     * @apiGroup 用户
     *
     * @apiParam {String} token 用户标识
     *
     * @apiSuccess {Int} code 返回码
     * @apiSuccess {String} message  提示信息
     * @apiSuccess {Object} data  返回数据
     *
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "code":0,
     *          "message":"",
     *          "data":{
     *              token 请求数据使用,
     *              userNumber 用户编号,
     *              userName 用户昵称,
     *              userSex 用户性别 1，女，2，男,
     *              userPic 用户头像,
     *              status 用户状态 1：正常。2：禁用,
     *              userStatus 是否是vip 1：非VIP，2：vip,
     *              address 地址,
     *              userPhone 用户手机号,
     *              userAmount 用户金额,
     *              startTime vip开始时间,
     *              endTime vip结束时间,
     *              trueName 用户姓名
     *             }
     *      }
     */
    public function getUserInfo(Request $request){
        $params = $request->param();
        $token = $this->checkParam($params['token'],'token',true);
        $user = Model('User');
        $res = $user->getInfo($token);
        if($res){
            return json(returnAjax($res));
        }else{
            return json(returnAjax('','系统异常','-1'));
        }
    }


     /**
     * @api {POST} User/userTakePhoto  用户拍照
     * @apiVersion 1.0.0
     * @apiGroup 用户
     *
     * @apiParam {String} id 用户id
     * @apiParam {String} photoPath   照片
     *
     * @apiSuccess {Int} code 返回码
     * @apiSuccess {String} message  提示信息
     * @apiSuccess {Object} data  返回数据
     *
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "code":0,
     *          "message":"",
     *          "data":{
     *              
     *             }
     *      }
     */
    public function userTakePhoto(){
        header("Content-type:text/html;charset=utf-8");
        $userId = $this->checkParam($_POST['id'],'id',true);
        $file = Request::file('photoPath');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'photo',$userId);
            if($info){
                $url = "photo/".$info->getFilename(); 
                $user = Model('User');
                $ph = $user->updPhoto($userId,$url);
                if($ph){
                    $res['photo'] = $url;
                    return json(returnAjax($res));
                }else{
                    return json(returnAjax('','系统错误','-1'));
                }
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }else{
            return json(returnAjax(null,'未获取到上传的图片',1));
        }
    }
}
